// pages/sale/refund/refund.js
var app = getApp()
const $api = require('../../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 0, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '申请退款', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    loadModal: true,
    reasonStatus: false, //是否确认选择了退款原因
    reasonid: '', //退款原因id
    CSreasonid: '', //选择的退款原因id
    CSreasonText: '',
    reasonText: '请选择', //退款原因
    desc: '', //描述
    images: [], //凭证图片
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    $api.getAsaleReason({
      login_token: wx.getStorageSync('login_token'),
      type: 1
    }).then((res) => {
      _this.setData({
        reason: res.data.data,
        goods: wx.getStorageSync('goods'),
        price: wx.getStorageSync('price'),
        fee: wx.getStorageSync('fee'),
        goodslistlength:wx.getStorageSync('goodslistlength'),
        loadModal: false,
        refund_fee:wx.getStorageSync('fee'),
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })

  },
  // 打开弹框
  showModal(e) {
    var _this = this;
    if (_this.data.reasonid != '') {
      _this.setData({
        CSreasonid: _this.data.reasonid,
        CSreasonText: _this.data.reasonText
      })
    }
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,
      CSreasonid: '',
      CSreasonText: '',
    })
  },
  //选择退款原因
  onChoice(e) {
    var _this = this;
    _this.setData({
      CSreasonid: e.currentTarget.dataset.id,
      CSreasonText: e.currentTarget.dataset.desc
    })
  },
  //确定选择退款原因
  onChoiceBtn() {
    var _this = this;
    if (_this.data.CSreasonid == '') {
      wx.showToast({
        title: '请选择退款原因',
        icon: 'none',
        duration: 2000
      })
    } else {
      _this.setData({
        reasonid: _this.data.CSreasonid,
        reasonText: _this.data.CSreasonText,
        modalName: null,
      })
    }
  },
  // 获取描述
  onDesc(e) {
    this.setData({
      desc: e.detail.value
    })
  },
  // 上传图片
  onImages(e) {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    $api.uploadImages().then((res) => {
      _this.setData({
        images: _this.data.images.concat(res.url),
        loadModal: false
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  //删除图片
  onClose(e) {
    var _this = this;
    var images = _this.data.images;
    images.splice(e.currentTarget.dataset.idx, 1);
    _this.setData({
      images: images
    })
  },
  //提交
  onSubmit() {
    var _this = this;
    if (_this.data.reasonid == '') {
      wx.showToast({
        title: '请选择退款原因',
        icon: 'none',
        duration: 2000
      })
    } else {
      let refund_fee = _this.data.refund_fee
      if (_this.data.fee&&!refund_fee&&(_this.data.goodslistlength == _this.data.goods.length||!_this.data.goodslistlength)) {
        wx.showToast({
          title: '请输入退款邮费',
          icon: 'none',
          duration: 2000
        })
        return
      }else{
        refund_fee = ''
      }
      _this.setData({
        loadModal: true
      })
      var details = [];
      var refund_money=0;
      for (var i = 0; i < _this.data.goods.length; i++) {
        details = details.concat(_this.data.goods[i].id)
        refund_money=refund_money+parseFloat(_this.data.goods[i].num*_this.data.goods[i].price)
      }
      details = JSON.stringify(details)
      console.log(refund_money)
      var images = JSON.stringify(_this.data.images);

      $api.submitAsale({
        login_token: wx.getStorageSync('login_token'),
        order_id: _this.data.goods[0].order_id,
        asale_type: 1,
        desc: _this.data.desc,
        images: images,
        reason_id: _this.data.reasonid,
        goods_status: 0,
        refund_money:refund_money,
        details: details,
        refund_fee:_this.data.refund_fee
      }).then((res) => {
        _this.setData({
          loadModal: false
        })
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 2000
        })

        setTimeout(function () {
          wx.redirectTo({
            url: '/pages/sale/refundDetail/refundDetail?id='+res.data.data.asale_id,
          })
        }, 1500)
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })


    }
  },
  //查看图片
  onSeeImage(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: [e.currentTarget.dataset.url]
    })
  },
  oninputfee(e){
    console.log(e)
    this.setData({
      refund_fee:e.detail.value
    })
  },
  onblurfee(e){
    console.log(e)
    if(e.detail.value>this.data.fee){
      wx.showToast({
        title: '超出支付邮费',
        icon: 'none',
        duration: 2000
      })
      this.setData({
        refund_fee:''
      })
    }
    
  },
})